//
//  UIViewController+ViewDidLoad.m
//  WProjectForNative
//
//  Created by HuangZhen on 16/9/11.
//  Copyright © 2016年 HuangZhen. All rights reserved.
//

#import "UIViewController+ViewDidLoad.h"
#import <objc/runtime.h>
#import "UIImage+Color.h"
#import "NSObject+Swizzling.h"
#import "UIViewController+TopViewController.h"

@interface UIViewController()
@property(nonatomic, assign) BOOL hasSetupStatusBarStyle;
@property(nonatomic, assign) UIStatusBarStyle originalStatusBarStyle;
@end

@implementation UIViewController (ViewDidLoad)

+ (void)load {
    [self swizzlingWithOriginalSEL:@selector(viewDidLoad)
                            newSEL:@selector(afterViewDidLoad)];
    
    [self swizzlingWithOriginalSEL:@selector(presentViewController:animated:completion:)
                            newSEL:@selector(mPresentViewController:animated:completion:)];
    //    [self swizzlingWithOriginalSEL:@selector(preferredStatusBarStyle)
    //                            newSEL:@selector(mPreferredStatusBarStyle)];
//    [self swizzlingWithOriginalSEL:@selector(viewDidAppear:)
//                            newSEL:@selector(viewDidAppearStatusBarStyleChange:)];
//    [self swizzlingWithOriginalSEL:@selector(viewWillDisappear:)
//                            newSEL:@selector(viewWillDisappearStatusBarStyleChange:)];
}

- (void)mPresentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion {
    dispatch_async(dispatch_get_main_queue(), ^{
        
        [self mPresentViewController:viewControllerToPresent animated:flag completion:completion];
        
        viewControllerToPresent.isPresentViewController = YES;
    });
}

- (void)afterViewDidLoad {
    [self afterViewDidLoad];
    self.originalStatusBarStyle = -1;
    if (self.navigationController && !self.navigationController.isPresentViewController) {
        self.extendedLayoutIncludesOpaqueBars = YES;
        self.automaticallyAdjustsScrollViewInsets = NO;
        self.edgesForExtendedLayout = UIRectEdgeAll;
    }
    
    if ([[[UIDevice currentDevice] systemVersion]floatValue] < 8.0) {
        if ([self respondsToSelector:@selector(extendedLayoutIncludesOpaqueBars)]) {
            self.extendedLayoutIncludesOpaqueBars = NO;
        }
        if ([self respondsToSelector:@selector(automaticallyAdjustsScrollViewInsets)]) {
            self.automaticallyAdjustsScrollViewInsets = YES;
        }
        if ([self respondsToSelector:@selector(edgesForExtendedLayout)]) {
            self.edgesForExtendedLayout = UIRectEdgeNone;
        }
    }
    
    UIBarButtonItem *temporaryBarButtonItem = [[UIBarButtonItem alloc] init];
    temporaryBarButtonItem.title =@"";
    self.navigationItem.backBarButtonItem = temporaryBarButtonItem;
    
    [self preferredStatusBarStyle];
    
}
//- (void)setPresentationControllerStatusBarStyleWithViewController:(UIViewController *)vc {
//    vc.statusBarStyle = self.statusBarStyle;
//    vc.originalStatusBarStyle = self.originalStatusBarStyle;
//    if (vc.parentViewController) {
//        [self setPresentationControllerStatusBarStyleWithViewController:vc.parentViewController];
//    }
//}
//
//- (void)viewDidAppearStatusBarStyleChange:(BOOL)animated {
//    if ([NSStringFromClass([self class]) hasPrefix:@"RCTWrapperViewController"]) {
//        if (!self.hasSetupStatusBarStyle) {
//            self.statusBarStyle = [UIApplication sharedApplication].statusBarStyle;
//        }
//        if ([[[UIDevice currentDevice] systemVersion]floatValue]<8.0) {
//            if (self.statusBarStyle == -1) {
//                self.statusBarStyle = UIStatusBarStyleLightContent;
//            }
//        }
//        [UIApplication sharedApplication].statusBarStyle = self.statusBarStyle;
//        
//    }else {
//        if (![NSStringFromClass([self class]) hasPrefix:@"UI"]) {//系统控件 过滤之
//            if (!self.hasSetupStatusBarStyle) {
//                self.statusBarStyle = UIStatusBarStyleLightContent;
//            }else {
//                self.originalStatusBarStyle = self.statusBarStyle;
//            }
//            [self setPresentationControllerStatusBarStyleWithViewController:self];
//            /*
//             为了防止再次进入VC的时候 self.originalStatusBarStyle 会受到上一个页面的影响，因此加入一个判断值来做判断‘VC是否是已经存在’
//             */
//            if (self.originalStatusBarStyle == -1) {
//                self.originalStatusBarStyle = [UIApplication sharedApplication].statusBarStyle;
//            }
//            if ([UIApplication sharedApplication].statusBarStyle != self.statusBarStyle) {
//                [[UIApplication sharedApplication]setStatusBarStyle:self.statusBarStyle animated:NO];
//            }
//        }
//    }
//    
//    
//    [self viewDidAppearStatusBarStyleChange:animated];
//}

//- (void)viewWillDisappearStatusBarStyleChange:(BOOL)animated {
//    
//    if ([[[UIDevice currentDevice] systemVersion]floatValue]<8.0) {
//        if (self.originalStatusBarStyle == -1) {
//            self.originalStatusBarStyle = UIStatusBarStyleLightContent;
//        }
//    }
//    if (![NSStringFromClass([self class]) hasPrefix:@"UI"]) {//系统控件 过滤之
//        if ([UIApplication sharedApplication].statusBarStyle != self.originalStatusBarStyle) {
//            [[UIApplication sharedApplication]setStatusBarStyle:self.originalStatusBarStyle animated:NO];
//        }
//    }
//    
//    [self viewWillDisappearStatusBarStyleChange:animated];
//}

//- (UIStatusBarStyle)mPreferredStatusBarStyle {
//    return UIStatusBarStyleLightContent;
//}

- (BOOL)isPresentViewController {
    return [objc_getAssociatedObject(self, @selector(isPresentViewController)) boolValue];
}

- (void)setIsPresentViewController:(BOOL)isPresentViewController {
    objc_setAssociatedObject(self, @selector(isPresentViewController), @(isPresentViewController), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (BOOL)hasSetupStatusBarStyle {
    return [objc_getAssociatedObject(self, @selector(hasSetupStatusBarStyle)) boolValue];
}

- (void)setHasSetupStatusBarStyle:(BOOL)hasSetupStatusBarStyle {
    objc_setAssociatedObject(self, @selector(hasSetupStatusBarStyle), @(hasSetupStatusBarStyle), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UIStatusBarStyle)statusBarStyle {
    return [objc_getAssociatedObject(self, @selector(statusBarStyle)) integerValue];
}

- (void)setStatusBarStyle:(UIStatusBarStyle)statusBarStyle {
    self.hasSetupStatusBarStyle = YES;
    objc_setAssociatedObject(self, @selector(statusBarStyle), @(statusBarStyle), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UIStatusBarStyle)originalStatusBarStyle {
    return [objc_getAssociatedObject(self, @selector(originalStatusBarStyle)) integerValue];
}

- (void)setOriginalStatusBarStyle:(UIStatusBarStyle)originalStatusBarStyle {
    objc_setAssociatedObject(self, @selector(originalStatusBarStyle), @(originalStatusBarStyle), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}


@end
